﻿using MediatR;
using SmartTracking.API.Write.Application.Commands.PoultryHouse_command;
using SmartTracking.Domain.Sheep;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.PoultryHouse_handler
{
    public class DeletePouitryHouseCommandHandler:IRequestHandler<DeletePouitryHouseCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<PoultryHouse> houseRep;

        public DeletePouitryHouseCommandHandler(
            IBaseRepository<PoultryHouse> houseRep)
        {
            this.houseRep = houseRep;
        }

        public async Task<ApiResult<int>> Handle(DeletePouitryHouseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.删除成功;
            result.Message = ApiEnum.删除成功.ToString();

            var list = houseRep.GetAll().FirstOrDefault(x => x.HouseId == request.HouseId);
            list.IsDeleted = true;
            var res = await houseRep.UpdateAsync(list);

            result.data = res;
            return await Task.FromResult(result);
        }
    }
}
